//
//  ViewController.swift
//  Dog Walk
//
//  Created by Pietro Rea on 7/17/15.
//  Copyright © 2015 Razeware. All rights reserved.
//

import UIKit
import CoreData
class ViewController: UIViewController, UITableViewDataSource {
  
    
    var managedContext: NSManagedObjectContext!
    
  lazy var dateFormatter: NSDateFormatter = {
    let formatter = NSDateFormatter()
    formatter.dateStyle = .ShortStyle
    formatter.timeStyle = .MediumStyle
    return formatter
  }()
    
    var currentDog: Dog!
  
  @IBOutlet var tableView: UITableView!
  var walks:Array<NSDate> = []
  
  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    
    tableView.registerClass(UITableViewCell.self,
      forCellReuseIdentifier: "Cell")
    let dogEntity = NSEntityDescription.entityForName("Dog", inManagedObjectContext: managedContext)
    let dogName = "Fido"
    let dogFetch = NSFetchRequest(entityName: "Dog")
    dogFetch.predicate = NSPredicate(format: "name == %@", dogName)
    do{
        let results = try managedContext.executeFetchRequest(dogFetch) as! [Dog]
        if results.count > 0 {
            currentDog = results.first
        }else {
            currentDog = Dog(entity: dogEntity!, insertIntoManagedObjectContext: managedContext)
            currentDog.name = dogName
            try managedContext.save()
        }
    }catch let error as NSError{
        print("Error:\(error)" + "description:\(error.localizedDescription)")
    }
  }
  
  func tableView(tableView: UITableView,
    numberOfRowsInSection section: Int) -> Int {
      
      return currentDog.walks!.count
  }
  
  func tableView(tableView: UITableView,
    titleForHeaderInSection section: Int) -> String? {
      return "List of Walks"
  }
  
  func tableView(tableView: UITableView,
    cellForRowAtIndexPath
    indexPath: NSIndexPath) -> UITableViewCell {
      
      let cell =
      tableView.dequeueReusableCellWithIdentifier("Cell",
        forIndexPath: indexPath) as UITableViewCell
      let walk = currentDog.walks![indexPath.row] as! Walk
      
      cell.textLabel!.text = dateFormatter.stringFromDate(NSDate(timeIntervalSince1970: walk.date))
      
      return cell
  }
  
  @IBAction func add(sender: AnyObject) {
//    walks.append(NSDate())
    tableView.reloadData()
  }
}

